/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.vale.controllers;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import org.vale.dao.AbstractFacade;
import org.vale.dao.CityDAO;
import org.vale.dao.ContactSiteDAO;
import org.vale.dao.ContactTypeDAO;
import org.vale.dao.SiteCategoryDAO;
import org.vale.dao.SiteDAO;
import org.vale.dao.StateDAO;
import org.vale.entity.City;
import org.vale.entity.ContactSite;
import org.vale.entity.ContactSitePK;
import org.vale.entity.ContactType;
import org.vale.entity.Site;
import org.vale.entity.SiteCategory;
import org.vale.entity.State;

/**
 *
 * @author Gabriel Bran gbran.u@gmail.com
 */
@ViewScoped
@Named(value = "siteBean")
public class SiteController extends Catalog<Site> implements java.io.Serializable {

    @EJB
    private SiteDAO siteDAO;
    @EJB
    private CityDAO cityDAO;
    @EJB
    private StateDAO stateDAO;
    @EJB
    private SiteCategoryDAO siteCategoryDAO;
    @EJB
    private ContactTypeDAO contactTypeDAO;
    @EJB
    private ContactSiteDAO contactSiteDAO;
    private List<City> cities;
    private List<State> states;
    private List<SiteCategory> categories;
    private List<ContactType> contactTypes;
    private String idState;
    private Integer idCity;
    private ContactSite currentContact;
    private boolean newProspective;
    private boolean active;
    private boolean nonInterested;
    private boolean tppGrantMou;
    private boolean pastorLeaderMou;
    private boolean registeredForTraining;

    @PostConstruct
    public void initialize() {
        Site site = getFromFlash("site");
        if (site != null) {
            setCurrent(site);
            getSelectedCheckBox();
            if (idState != null) {
                setCities(cityDAO.findByState(idState));
            } else {
                setCities(new ArrayList<City>());
            }
            setAction(Catalog.ACTION_UPDATE);
        } else {
            setAction(Catalog.ACTION_CREATE);
            initializeBean();
        }
        setStates(stateDAO.findAll());
        setCategories(siteCategoryDAO.findAll());
        setContactTypes(contactTypeDAO.findAll());
        initializeContact();
    }

    @Override
    protected void initializeBean() {
        setCurrent(new Site());
        getCurrent().setCategory(new SiteCategory());
        setCities(new ArrayList<City>());
        newProspective = false;
        active = false;
        nonInterested = false;
        pastorLeaderMou = false;
        registeredForTraining = false;
        tppGrantMou = false;
    }

    public void initializeContact() {
        setCurrentContact(new ContactSite(new ContactSitePK()));
        getCurrentContact().setCodcontacttype(new ContactType());
    }

    @Override
    protected void validate() throws Exception {
        setSelectedCheckBox();
        if (getAction().equalsIgnoreCase(Catalog.ACTION_CREATE)) {
            getCurrent().setIdsite(siteDAO.getNewSiteId(idState));
            getCurrent().setDatecreated(new Date());
        } else {
            getCurrent().setDatemodified(new Date());
        }
    }

    public void setSelectedCheckBox() {
        getCurrent().setNewprospective(newProspective ? "Y" : "N");
        getCurrent().setActive(active ? "Y" : "N");
        getCurrent().setNoninterested(nonInterested ? "Y" : "N");
        getCurrent().setPastormou(pastorLeaderMou ? "Y" : "N");
        getCurrent().setRegtraining(registeredForTraining ? "Y" : "N");
        getCurrent().setTtpmou(tppGrantMou ? "Y" : "N");
        getCurrent().setIdcity(new City(idCity));
    }

    public void getSelectedCheckBox() {
        newProspective = (getCurrent().getNewprospective() == null ? false : getCurrent().getNewprospective().equals("Y"));
        active = (getCurrent().getActive() == null ? false : getCurrent().getActive().equals("Y"));
        nonInterested = (getCurrent().getNoninterested() == null ? false : getCurrent().getNoninterested().equals("Y"));
        pastorLeaderMou = (getCurrent().getPastormou() == null ? false : getCurrent().getPastormou().equals("Y"));
        registeredForTraining = (getCurrent().getRegtraining() == null ? false : getCurrent().getRegtraining().equals("Y"));
        tppGrantMou = (getCurrent().getTtpmou() == null ? false : getCurrent().getTtpmou().equals("Y"));
        idCity = (getCurrent().getIdcity() == null ? 0 : getCurrent().getIdcity().getIdcity());
        idState = (getCurrent().getIdcity() == null ? "" : getCurrent().getIdcity().getIdstate().getIdstate());
    }

    @Override
    protected AbstractFacade getAbstractFacade() {
        return siteDAO;
    }

    public void onStateChange() {
        if (idState != null) {
            setCities(cityDAO.findByState(idState));
        } else {
            setCities(new ArrayList<City>());
        }
    }

    public String saveSiteContact() {
        if (getCurrent() == null) {
            addErrorMessage("Please select a Site");
            return null;
        }

        if ((getCurrent().getContactSiteList() == null) || (getCurrent().getContactSiteList().isEmpty())) {
            getCurrent().setContactSiteList(new ArrayList<ContactSite>());
        }
        currentContact.getContactSitePK().setIdsite(getCurrent().getIdsite());
        currentContact.getContactSitePK().setIdcontact(contactSiteDAO.newContactTypeId(getCurrent().getIdsite()));
        for (ContactType ct : contactTypes) {
            if (ct.getCodcontacttype().equals(currentContact.getCodcontacttype().getCodcontacttype())) {
                currentContact.setCodcontacttype(ct);
                break;
            }
        }
        getCurrent().getContactSiteList().add(currentContact);
        save();
        initializeContact();
        return null;
    }

    public String deleteSiteContact() {
        if (getCurrent() == null) {
            addErrorMessage("Please select a Site");
            return null;
        }
        if (getCurrentContact() == null) {
            addErrorMessage("Please select Site Contact");
            return null;
        }
        getCurrent().getContactSiteList().remove(currentContact);
        save();
        initializeContact();
        return null;
    }

    public ContactSite getCurrentContact() {
        return currentContact;
    }

    public void setCurrentContact(ContactSite currentContact) {
        this.currentContact = currentContact;
    }

    public List<ContactType> getContactTypes() {
        return contactTypes;
    }

    public void setContactTypes(List<ContactType> contactTypes) {
        this.contactTypes = contactTypes;
    }

    public List<SiteCategory> getCategories() {
        return categories;
    }

    public void setCategories(List<SiteCategory> categories) {
        this.categories = categories;
    }

    public boolean isRegisteredForTraining() {
        return registeredForTraining;
    }

    public void setRegisteredForTraining(boolean registeredForTraining) {
        this.registeredForTraining = registeredForTraining;
    }

    public boolean isPastorLeaderMou() {
        return pastorLeaderMou;
    }

    public void setPastorLeaderMou(boolean pastorLeaderMou) {
        this.pastorLeaderMou = pastorLeaderMou;
    }

    public boolean isTppGrantMou() {
        return tppGrantMou;
    }

    public void setTppGrantMou(boolean tppGrantMou) {
        this.tppGrantMou = tppGrantMou;
    }

    public Integer getIdCity() {
        return idCity;
    }

    public void setIdCity(Integer idCity) {
        this.idCity = idCity;
    }

    public List<City> getCities() {
        return cities;
    }

    public void setCities(List<City> cities) {
        this.cities = cities;
    }

    public List<State> getStates() {
        return states;
    }

    public void setStates(List<State> states) {
        this.states = states;
    }

    public String getIdState() {
        return idState;
    }

    public void setIdState(String idState) {
        this.idState = idState;
    }

    public boolean isNewProspective() {
        return newProspective;
    }

    public void setNewProspective(boolean newProspective) {
        this.newProspective = newProspective;
    }

    public boolean isActive() {
        return active;
    }

    public void setActive(boolean active) {
        this.active = active;
    }

    public boolean isNonInterested() {
        return nonInterested;
    }

    public void setNonInterested(boolean nonInterested) {
        this.nonInterested = nonInterested;
    }

}
